package com.xuliugen.security.web.controller;

import com.fasterxml.jackson.annotation.JsonView;
import com.xuliugen.security.web.dto.User;
import com.xuliugen.security.web.dto.command.UserCommand;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * User相关API
 * Created by xuliugen on 2017/11/4.
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    /**
     * @param userCommand 用户查询命令
     * @param pageable    分页条件
     * @return
     */
    @GetMapping
    @JsonView(User.UserSimpleView.class)
    @ApiOperation(value = "用户查询操作")
    public List<User> query(UserCommand userCommand,
                            @PageableDefault(page = 1, size = 10, sort = "user_name,asc") Pageable pageable) {
        System.out.println(userCommand.getUserName() + ":" + userCommand.getUserPassword());
        System.out.println(pageable.getPageSize());
        System.out.println(pageable.getPageNumber());
        System.out.println(pageable.getSort());
        System.out.println(pageable.getOffset());

        List<User> users = new ArrayList<>(3);
        users.add(new User());
        users.add(new User());
        users.add(new User());

        return users;
    }

    /**
     * @param id 正则表达式只能是数字
     * @return
     */
    @GetMapping("/{id:\\d+}")
    @JsonView(User.UserDetailView.class)
    @ApiOperation(value = "根据用户ID获取用户信息")
    public User getInfo(@ApiParam(value = "用户ID") @PathVariable String id) {
        User user = new User();
        user.setUsername("xuliugen");
        return user;
    }

    /**
     * 创建用户
     * 即使不使用BindingResult也可以返回接口的调用者错误信息
     * @param user
     * @return
     */
    @PostMapping
    @ApiOperation(value = "创建用户")
    public User create(@Valid @RequestBody User user) {
        System.out.println(user.getId());
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        System.out.println(user.getBirthday());

        user.setId("1");
        return user;
    }

    /**
     * 即使不使用BindingResult也可以返回接口的调用者错误信息
     * @param user
     * @param errors
     * @return
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "创建用户")
    public User createByException(@Valid @RequestBody User user, BindingResult errors) {
        if (errors.hasErrors()) {
            errors.getAllErrors().forEach(error -> System.out.println(error.getDefaultMessage()));
        }

        System.out.println(user.getId());
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        System.out.println(user.getBirthday());

        user.setId("1");
        return user;
    }

    /**
     * 更新用户
     * @param user
     * @param errors
     * @return
     */
    @PutMapping("/{id:\\d+}")
    @ApiOperation(value = "更新用户")
    public User update(@Valid @RequestBody User user, BindingResult errors) {
        if (errors.hasErrors()) {
            errors.getAllErrors().forEach(error -> {
                FieldError fieldError = (FieldError) error;
                System.out.println(fieldError.getField() + ":" + error.getDefaultMessage());
            });
        }

        System.out.println(user.getId());
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        System.out.println(user.getBirthday());

        user.setId("1");
        return user;
    }

    @DeleteMapping("/{id:\\d+}")
    @ApiOperation(value = "删除用户")
    public void delete(@PathVariable String id) {
        System.out.println(id);
    }
}
